<?php 
	include_once "functions.php";	

	function teacher_add_1($data){
		if( invalidUser()){
				return false;
		}		
		global $wpdb;

		// generate login 
		//  $wpdb->users 

		$res = $wpdb->get_row( "SELECT max(id) maxid FROM wp_users" , ARRAY_A);
		$maxid = $res["maxid"];
		$maxid = $maxid + 1;
		$login = "teacher" . $maxid;

		$pass = generatePass(8);

		return Array(
				"fio"=>"FIO",
				"login"=>$login,
				"pwd"=>$pass,
				"maxid"=>$maxid
			);

	}
	function teacher_add_2($data){
		if( invalidUser()){
				return false;
		}		
		global $wpdb;
		return Array(
				"fio"=>"FIO",
				"login"=>"Login1",
				"pwd"=>"Pass1",
			);

	}
		function teacher_add_3($data){
		if( invalidUser()){
				return false;
		}		
		global $wpdb;
		return Array(
				"fio"=>"FIO",
				"login"=>"Login1",
				"pwd"=>"Pass1",
			);

	}
		function teacher_add_4($data){
		if( invalidUser()){
				return false;
		}		
		global $wpdb;
		return Array(
				"fio"=>"FIO",
				"login"=>"Login1",
				"pwd"=>"Pass1",
			);

	}
	function teacher_add_5($data){
		if( invalidUser()){
				return false;
		}		
		global $wpdb;
		return Array(
				"fio"=>"FIO",
				"login"=>"Login1",
				"pwd"=>"Pass1",
			);

	}
	function teacher_add_final($data){
		if( invalidUser()){
				return false;
		}		
		
		$user = UsersORM::getUser();	
		$prefix = "timetable_";
		
		/*
			add_courses: Array[0]
			adress: ""
			birthday: ""
			courses: Array[0]
			email: ""
			fio: ""
			login: ""
			phone: ""
			pwd: ""
			sex: "М"
		*/

		global $wpdb;



		//  create wp user
		/*
			$user_name = $data['client_name'];
			$user_fullname = $data['client_fullname'];
			$user_password = $data['client_password'];
			$user_password2 = $data['client_password2'];
			$user_email = $data['client_email'];
			$role_status = $data['role'];
		*/

		//  create user + role		
		//  teacher



		$res = $wpdb->get_row( "SELECT max(id) maxid FROM wp_users" , ARRAY_A);
		$maxid = $res["maxid"];
		$maxid = $maxid + 1;
		$login = "teacher" . $maxid;
		$pass = generatePass(8);	

		$data_regwp = Array(
				'client_name' => $login,
				'client_fullname' => $data['fio'],
				'client_password' => $pass,
				'client_password2' => $pass,
				'client_email'=>$data['email'],
				'role' => 3
			);

	    $res_id = new_user($data_regwp, 1);

	    $user_id = $res_id['user_id'];
	    $teacher_id = $res_id['teacher_id'];

	    echo "teacher_id ". $teacher_id . "\t";

	    //var_dump($data);

	    if(isset($data['courses'])  && is_array($data['courses']) ){
		    foreach ($data['courses'] as $course) {
		    	$course_id = $course['course_id'];
				$wpdb->insert(
						$prefix.'teachercourse',
						array(
							'teacher_id'=>$teacher_id,
							'course_id'=>$course_id
						),
						array(
							'%d',
							'%d'
						)
					);
			}
	    }

	    if(isset($data['add_courses'])  && is_array($data['add_courses']) ){
			foreach ($data['add_courses'] as $course) {
				$course_id = $course['course_id'];
				$wpdb->insert(
						$prefix.'teachercourse',
						array(
							'teacher_id'=>$teacher_id,
							'course_id'=>$course_id,
							'teachercourse_main'=>0
						),
						array(
							'%d',
							'%d',
							'%d'
						)
					);
			}
		}




		/*$user_id_q = "SELECT user_id FROM ${prefix}timetable_teacher WHERE idteacher = $teacher_id";
		$res = $wpdb->get_row( $user_id_q, ARRAY_A );
		$user_id = $res['user_id'];*/

		//echo $user_id_q;

		echo "$user_id ".$user_id."\t";

		echo "add2";


		$birthday = $data['birthday'];
		$sex = $date['sex'];


		$ins_q = "INSERT INTO ${prefix}userinfo 
		    (user_id,userinfo_sex, userinfo_birthday, userinfo_phone, userinfo_adress) 
		    VALUES( 
		    	$user_id, 
		    	'{$sex}', 
		    	STR_TO_DATE( '{$birthday}' , '%d-%m-%Y'), 
		    	'{$data['phone']}', 
		    	'{$data['adress']}' 
		    )";
		echo $ins_q;

		$wpdb->query($ins_q);

		/*
		$user_info_q =  "INSERT "  

		DATE_FORMAT(clientcourse_begin,'%d-%m-%Y')
		*/

		return Array(
				"login"=> $login ,
				"pwd"=>  $pass,
			);

	}


   function new_user($data, $return_id){
			$prefix = "timetable_";
		
			$user_name = $data['client_name'];
			$user_fullname = $data['client_fullname'];
			$user_password = $data['client_password'];
			$user_password2 = $data['client_password2'];
			$user_email = $data['client_email'];
			$role_status = $data['role'];
			
			if($role_status == '') {
				$role_status = 5;
			}

			$user_id = username_exists( $user_name );
	
			global $wpdb;
			if ( !$user_id and email_exists($user_email) == false and $user_password == $user_password2 ) {


				echo "addreg1"."\n";

				$wp_user_id = wp_insert_user( 
						array(
							"user_login" => $user_name, 
							"user_pass" => $user_password, 
							"display_name" => $user_fullname, 
							"user_email" => $user_email)
					);


				echo $wp_user_id ."\n";

				$role_query = "SELECT idrole role_id,  role_name, role_status FROM {$prefix}role WHERE role_status = $role_status";
				$role = $wpdb->get_row($role_query, ARRAY_A);	
				$role_id = $role['role_id'];
				
				$wpdb->insert(
						"${prefix}user",
						array(
								"role_id"=> $role_id,
								"wp_user_id" =>$wp_user_id
							),
						array(
								'%d',
								'%d'
							)
					);
				
				$user_id =  $wpdb->insert_id;
				
				echo "user_id ".$user_id."\n";

				$res = array(
						"user_id"=>$user_id
					);

				$wpdb->insert(
						"{$prefix}teacher",
						array(
								"teacher_name"=> $user_fullname,
								"user_id" =>$user_id
							),
						array(
								'%s',
								'%d'
							)
					);
				$teacher_id =  $wpdb->insert_id;
				
				$res['teacher_id'] = $teacher_id;


				echo "\n";
				var_dump($res);
				echo "\n";

				return $res;

			}
	}


?>